from src.core.models.dto.activation_code import ActivationCodeDTO
from src.core.models.enums import SubscriptionType
from src.database.models import ActivationCode
from src.database.uow import SQLAlchemyUoW


class ActivationCodeService:
    def __init__(self, uow: SQLAlchemyUoW):
        self._uow = uow

    async def get_activation_code_by_name(
        self, code_name: str, subscription_type: SubscriptionType
    ) -> ActivationCodeDTO:
        """Получаем код активации по имени"""

        return await self._uow.activation_code_reader.get_activation_code_by_name(
            code_name, subscription_type
        )

    async def increase_activation_code_uses(
        self, activation_code_id: int, uses_number: int = 1
    ) -> None:
        activation_code: ActivationCode = (
            await self._uow.activation_code_repo.acquire_activation_code_by_id(
                activation_code_id
            )
        )
        activation_code.uses_number += uses_number

        await self._uow.activation_code_repo.update_activation_code(activation_code)

        await self._uow.commit()
